"""
主应用窗口类
"""
import tkinter as tk
from tkinter import messagebox

from .components import MenuForm, ActionButtons


class RightClickManagerApp:
    """右键菜单管理器主应用"""

    def __init__(self):
        self.root = tk.Tk()
        self.setup_window()
        self.create_widgets()
        self.bind_events()

    def setup_window(self):
        """设置窗口属性"""
        self.root.title("Windows右键菜单管理器")

        # 设置窗口大小
        window_width = 400
        window_height = 270

        # 获取屏幕尺寸并居中
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()
        x_position = (screen_width - window_width) // 2
        y_position = (screen_height - window_height) // 2

        self.root.geometry(f"{window_width}x{window_height}+{x_position}+{y_position}")
        self.root.resizable(width=False, height=False)
        self.root.configure(bg="#f0f0f0")

    def create_widgets(self):
        """创建界面组件"""
        # 创建菜单表单
        self.menu_form = MenuForm(self.root)
        self.menu_form.pack(pady=10, padx=20, fill=tk.X)

        # 创建操作按钮
        self.action_buttons = ActionButtons(self.root)
        self.action_buttons.pack(pady=20)

    def bind_events(self):
        """绑定事件"""
        # 绑定回车键到添加操作
        self.root.bind('<Return>', lambda event: self.action_buttons.on_add_click())

        # 设置按钮回调函数
        self.action_buttons.set_callbacks(
            on_add=self.on_add_menu,
            on_delete=self.on_delete_menu
        )

    def on_add_menu(self, menu_name, command_path):
        """添加菜单回调函数"""
        from registry.manager import RegistryManager

        registry_manager = RegistryManager()
        success = registry_manager.add_menu(menu_name, command_path)

        if success:
            messagebox.showinfo("提示", "右键菜单添加成功")
            self.menu_form.clear_form()
        return success

    def on_delete_menu(self, menu_name):
        """删除菜单回调函数"""
        from registry.manager import RegistryManager

        registry_manager = RegistryManager()
        success = registry_manager.delete_menu(menu_name)

        if success:
            messagebox.showinfo("提示", "右键菜单删除成功")
            self.menu_form.clear_form()
        return success

    def create_menus(self):
        """创建菜单栏"""
        menubar = tk.Menu(self.root)
        self.root.config(menu=menubar)

        # 调试菜单
        debug_menu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="调试", menu=debug_menu)
        debug_menu.add_command(label="列出所有菜单", command=self.list_all_menus)

    def list_all_menus(self):
        """列出所有已注册的菜单"""
        from registry.manager import RegistryManager
        registry_manager = RegistryManager()
        menus = registry_manager.list_menus()

        if menus:
            menu_list = "\n".join(menus)
            messagebox.showinfo("已注册的菜单", f"找到 {len(menus)} 个菜单:\n{menu_list}")
        else:
            messagebox.showinfo("已注册的菜单", "没有找到任何菜单")

    def run(self):
        """运行应用"""
        self.root.mainloop()